#!/bin/bash
# Check that cobbler-settings can modify value from the YAML

# Arrange
source ${SYSTESTS_PRELUDE} && prepare

set -x -e -o pipefail

TEST_CONFIG="${tmp}/settings-dict.yaml"
EXPECTED_CONFIG="${tmp}/settings-expected.yaml"
cat <<-END >$TEST_CONFIG
mgmt_parameters:
 from_cobbler: true

auto_migrate_settings: false
allow_duplicate_hostnames: false
allow_duplicate_ips: false
allow_duplicate_macs: false
allow_dynamic_settings: false
anamon_enabled: false
authn_pam_service: "login"
auth_token_expiration: 3600
autoinstall_snippets_dir: /var/lib/cobbler/snippets
autoinstall_templates_dir: /var/lib/cobbler/templates
boot_loader_conf_template_dir: "/etc/cobbler/boot_loader_conf"
bootloaders_dir: "/var/lib/cobbler/loaders"
bootloaders_formats:
  aarch64:
    binary_name: grubaa64.efi
  arm:
    binary_name: bootarm.efi
  arm64-efi:
    binary_name: grubaa64.efi
    extra_modules:
      - efinet
  i386-efi:
    binary_name: bootia32.efi
  i386-pc-pxe:
    binary_name: grub.0
    mod_dir: i386-pc
    extra_modules:
      - chain
      - pxe
      - biosdisk
  i686:
    binary_name: bootia32.efi
  IA64:
    binary_name: bootia64.efi
  powerpc-ieee1275:
    binary_name: grub.ppc64le
    extra_modules:
      - net
      - ofnet
  x86_64-efi:
    binary_name: grubx86.efi
    extra_modules:
      - chain
      - efinet
bootloaders_modules:
  - btrfs
  - ext2
  - xfs
  - jfs
  - reiserfs
  - all_video
  - boot
  - cat
  - configfile
  - echo
  - fat
  - font
  - gfxmenu
  - gfxterm
  - gzio
  - halt
  - iso9660
  - jpeg
  - linux
  - loadenv
  - minicmd
  - normal
  - part_apple
  - part_gpt
  - part_msdos
  - password_pbkdf2
  - png
  - reboot
  - search
  - search_fs_file
  - search_fs_uuid
  - search_label
  - sleep
  - test
  - "true"
  - video
  - mdraid09
  - mdraid1x
  - lvm
  - serial
  - regexp
  - tr
  - tftp
  - http
  - luks
  - gcry_rijndael
  - gcry_sha1
  - gcry_sha256
syslinux_dir: '/usr/share/syslinux'
syslinux_memdisk_folder: '/usr/share/syslinux'
syslinux_pxelinux_folder: '/usr/share/syslinux'
grub2_mod_dir: '/usr/share/grub2'
bootloaders_shim_folder: '/usr/share/efi/*/'
bootloaders_shim_file: 'shim\.efi'
bootloaders_ipxe_folder: '/usr/share/ipxe/'
build_reporting_enabled: false
build_reporting_sender: ""
build_reporting_email: [ 'root@localhost' ]
build_reporting_smtp_server: "localhost"
build_reporting_subject: ""
build_reporting_ignorelist: []
cheetah_import_whitelist:
 - "random"
 - "re"
 - "time"
 - "netaddr"
createrepo_flags: "-c cache -s sha"
autoinstall: "default.ks"
default_name_servers: []
default_name_servers_search: []
default_ownership:
 - "admin"
default_password_crypted: "$1$mF86/UHC$WvcIcX2t6crBz2onWxyac."
default_template_type: "cheetah"
default_virt_bridge: xenbr0
default_virt_file_size: 5.0
default_virt_ram: 512
default_virt_type: xenpv
enable_ipxe: false
enable_menu: true
http_port: 80
kernel_options: {}
ldap_server: "ldap.example.com"
ldap_base_dn: "DC=example,DC=com"
ldap_port: 389
ldap_tls: true
ldap_anonymous_bind: true
ldap_search_bind_dn: ''
ldap_search_passwd: ''
ldap_search_prefix: 'uid='
ldap_tls_cacertfile: ''
ldap_tls_keyfile: ''
ldap_tls_certfile: ''
ldap_tls_cacertdir: ''
ldap_tls_cipher_suite: ''
ldap_tls_reqcert: ''
mgmt_classes: []
puppet_auto_setup: false
sign_puppet_certs_automatically: false
puppetca_path: "/usr/bin/puppet"
remove_old_puppet_certs_automatically: false
puppet_server: 'puppet'
puppet_version: 2
puppet_parameterized_classes: true
manage_dhcp: false
manage_dhcp_v6: false
manage_dhcp_v4: false
next_server_v4: 127.0.0.1
next_server_v6: "::1"
manage_dns: false
bind_chroot_path: ""
bind_zonefile_path: "/var/lib/named"
bind_master: 127.0.0.1
manage_forward_zones: []
manage_reverse_zones: []
manage_tftpd: true
tftpboot_location: "/var/lib/tftpboot"
manage_rsync: false
power_management_default_type: 'ipmilanplus'
pxe_just_once: true
nopxe_with_triggers: true
redhat_management_server: "xmlrpc.rhn.redhat.com"
redhat_management_permissive: false
redhat_management_key: ""
register_new_installs: false
reposync_flags: "--newest-only --delete --refresh --remote-time"
reposync_rsync_flags: "-rltDv --copy-unsafe-links"
restart_dns: true
restart_dhcp: true
run_install_triggers: true
scm_track_enabled: false
scm_track_mode: "git"
scm_track_author: "cobbler <cobbler@localhost>"
scm_push_script: "/bin/true"
server: 127.0.0.1
client_use_localhost: false
client_use_https: false
virt_auto_boot: true
webdir: "/var/www/cobbler"
webdir_whitelist:
  - misc
  - web
  - webui
  - localmirror
  - repo_mirror
  - distro_mirror
  - images
  - links
  - pub
  - repo_profile
  - repo_system
  - svc
  - rendered
  - .link_cache
xmlrpc_port: 25151
yum_post_install_mirror: true
yum_distro_priority: 1
yumdownloader_flags: "--resolve"
serializer_pretty_json: false
replicate_rsync_options: "-avzH"
replicate_repo_rsync_options: "-avzH"
always_write_dhcp_entries: false
proxy_url_ext: ""
proxy_url_int: ""
jinja2_includedir: "/var/lib/cobbler/jinja2"
convert_server_to_ip: false
buildisodir: "/var/cache/cobbler/buildiso"
cobbler_master: ""
default_virt_disk_driver: "raw"
grubconfig_dir: "/var/lib/cobbler/grub_config"
iso_template_dir: "/etc/cobbler/iso"
signature_path: "/var/lib/cobbler/distro_signatures.json"
signature_url: "https://cobbler.github.io/signatures/3.0.x/latest.json"
include: [ "/code/tests/test_data/V3_3_3/settings.d/*.settings" ]
END

cat <<-END >$EXPECTED_CONFIG
# Cobbler settings file
# Docs for this file can be found at: https://cobbler.readthedocs.io/en/latest/cobbler-conf.html

mgmt_parameters: {}
END

TEST_KEY="mgmt_parameters"
TEST_VALUE=""

# Act
cobbler-settings -c "$TEST_CONFIG" modify -k "$TEST_KEY" -v "$TEST_VALUE"

# Assert
grep "$TEST_KEY: {}" $TEST_CONFIG
